/*
 * NewNormalArcEventHandler.java
 *
 * Created on 10 czerwiec 2007, 17:23
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package edu.uj.petri.controller;

import edu.uj.petri.commands.AddArcCommand;
import edu.uj.petri.commands.Command;
import edu.uj.petri.document.Document;
import edu.uj.petri.view.ArcView;
import edu.uj.petri.view.DiagramElement;
import edu.uj.petri.view.NodeView;
import edu.uj.petri.view.NormalArcView;
import edu.uj.petri.view.PlaceView;
import edu.uj.petri.view.TransitionView;
import java.awt.event.KeyEvent;
import java.awt.geom.Point2D;

/**
 *
 * @author Paweł
 */
public class NewNormalArcEventHandler implements EventHandler {
    
    private DiagramElement begin, end;
    private ArcView arcView;
    public static ArcView unfinished = null;
    
    public NewNormalArcEventHandler()
    {
        Document.getActiveDocument().getView().selectNone();
        if (NewInhibitorArcEventHandler.unfinished != null) 
        {
            Document.getActiveDocument().getView().removeElement(NewInhibitorArcEventHandler.unfinished);
            Document.getActiveDocument().getView().draw();
            NewInhibitorArcEventHandler.unfinished = null;
            
        } else if (NewNormalArcEventHandler.unfinished != null)  
        {
            Document.getActiveDocument().getView().removeElement(NewNormalArcEventHandler.unfinished);
            Document.getActiveDocument().getView().draw();
            NewNormalArcEventHandler.unfinished = null;
        }
    }
    
    public void click(double x, double y) {
        
        DiagramElement e = Document.getActiveDocument().getView().getClickedElement( new Point2D.Double( x, y ), true);
        
        if (e == null ) return;
        
        if (begin == null)
        {
            begin = e;
            arcView = new NormalArcView( ((NodeView)e) );
            unfinished = arcView;
            Document.getActiveDocument().getView().addElement( arcView );
        }
        else
        {
            if (((e instanceof TransitionView) && (begin instanceof PlaceView)) 
                || ((e instanceof PlaceView) && (begin instanceof TransitionView)))
            {
                end = e;
                Command c = new AddArcCommand( arcView, Document.getActiveDocument().getView(), (NodeView)end);
                Document.getActiveDocument().execute( c );                
                begin = null;
                unfinished = null;
            }
        }
    }

    public void doubleClick(double x, double y) {
    }

    public void mouseMove(double x1, double y1, int button) {
        
        if (begin != null)
        {
            arcView.moveEnd( new Point2D.Double( x1, y1 ));
            Document.getActiveDocument().getView().draw();                
        }
    }

    public void mouseButtonDown(double x, double y) {

    }

    public void mouseButtonUp(double x, double y) {

    }

    public void rightClick(double x, double y) {
    }

    public void keyPressed(int keyCode) {
        
        if (keyCode == KeyEvent.VK_ESCAPE) 
        {
            begin = null;
            unfinished = null;
            Document.getActiveDocument().getView().removeElement( arcView );
        }
    }

    public void mouseDragged(double x, double y) {
    }

}
